package org.core.worker;

import java.io.IOException;

import org.gearman.Gearman;
import org.gearman.GearmanFunction;
import org.gearman.GearmanFunctionCallback;
import org.gearman.GearmanServer;
import org.gearman.GearmanWorker;
import org.core.worker.ReadIni;

/**
 *
 * @author isaiah
 *
 */
public class AppWorker implements GearmanFunction {

	/** The echo function name */
	//public static final String ECHO_FUNCTION_NAME = "echo";

	/** The host address of the job server */
	//public static final String ECHO_HOST = "localhost";

	/** The port number the job server is listening on */
	//public static final int ECHO_PORT = 4730;


	/**
	 * Creates an echo worker. A worker receives jobs from the job server distributes them
	 * to its registered functions.
	 *
	 * @param args
	 * 		N/A
	 * @throws IOException
	 * 		If an IO exception occurs during execution
	 */
	public static void main(String... args) {
		initialize();
	  
	}

	@Override
	public byte[] work(String function, byte[] data, GearmanFunctionCallback callback) throws Exception {

		/*
		 * The work method performs the gearman function. In this case, the echo function simply returns
		 * the data it received
		 */

		return data;
	}
	
	/**
	 * initialize config file
	 * to its registered functions.
	 *
	 * @param args
	 * 		N/A
	 * @throws 
	 * 		
	 */
	
	private static void initialize(){
		Gearman gearman = Gearman.createGearman();
		GearmanServer server = gearman.createGearmanServer(ReadIni.getMasterServer(),ReadIni.getServerPort());
		GearmanWorker worker = gearman.createGearmanWorker();
		worker.addFunction(ReadIni.getFunctionName(), new Ping());
		worker.addServer(server);
	}
	
	
	public static String test(){
		System.out.println("test");
		return "this is a successful Come on baby!";
	}
	
	

}